<?php

//Includes
require_once("MasterPage.php");
require_once("vcl/vcl.inc.php");
require_once("DB.php");
require_once("AdminLogin.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Org extends MasterPage
{
   public $pnMessage = null;
   public $Label1 = null;
   public $ibEmail = null;
   public $hfPublishRLName = null;
   public $hfPublishSLName = null;
   public $hfPublishEmail = null;
   public $ibSLName = null;
   public $ibRLName = null;
   public $Label2 = null;
   public $mDescription = null;
   public $lbNotecardKey = null;
   public $lbLogoFileName = null;
   public $lbSLURL = null;
   public $lbURL = null;
   public $lbDescription = null;
   public $lbName = null;
   public $lbUserProfile = null;
   public $lblOrgProfile = null;
   public $edNotecardKey = null;
   public $edLogoFileName = null;
   public $edSLURL = null;
   public $edURL = null;
   public $edName = null;
   public $btSubmit = null;
   public $cbCategory = null;
   public $pnOrg = null;
   public $Label7 = null;
   public $edFirstName = null;
   public $pnPerson = null;
   public $lbUUID = null;
   public $edUUID = null;
   public $edEmail = null;
   public $lbEmail = null;
   public $lbTitle = null;
   public $edTitle = null;
   public $lbSLLastName = null;
   public $lbSLFirstName = null;
   public $lbLastName = null;
   public $lbFistName = null;
   public $edSLLastName = null;
   public $edSLFirstName = null;
   public $edLastName = null;

   function ibEmailClick($sender, $params)
   {
      $action = GetSessionVar('action');

      if($action == "edit")
      {
         $state = $this->hfPublishEmail->Value;

         if($state == "0")
            $state = "1";
         else
            $state = "0";

         $this->hfPublishEmail->Value = $state;
      }
   }

   function ibSLNameClick($sender, $params)
   {
      $action = GetSessionVar('action');

      if($action == "edit")
      {
         $state = $this->hfPublishSLName->Value;

         if($state == "0")
            $state = "1";
         else
            $state = "0";

         $this->hfPublishSLName->Value = $state;
      }
   }


   function ibRLNameClick($sender, $params)
   {
      $action = GetSessionVar('action');

      if($action == "edit")
      {
         $state = $this->hfPublishRLName->Value;

         if($state == "0")
            $state = "1";
         else
            $state = "0";

         $this->hfPublishRLName->Value = $state;
      }
   }

   function btCancelClick($sender, $params)
   {
      SetSessionVar("action", "view");
   }

   function OrgCreate($sender, $params)
   {
      global $AdminLogin;
      $AdminLogin->EnsureLogin();
   }

   function personBeforeShow($sender, $params)
   {
      global $DB;

      //Get the action to perform
      if(!isset($_REQUEST['action']))
      {
         $action = GetSessionVar('action');
      }
      else
      {
         $action = $_REQUEST['action'];
         SetSessionVar("action", $action);
      }

      //Get the unique ID for this person
      if(!isset($_REQUEST['pid']))
      {
         $pid = GetSessionVar('pid');
      }
      else
      {
         $pid = $_REQUEST['pid'];
         SetSessionVar("pid", $pid);
      }

      if($action == "view" || $action = "edit")
      {

         if($action == "view")
         {

            // Populate the user info portion of the form
            $query = "SELECT * FROM person WHERE iPersonID = " . $pid;

            $DB->q_query->SQL = $query;
            $DB->q_query->Open();

            $this->edFirstName->Text = $DB->q_query->vchFirstName;
            $this->edTitle->Text = $DB->q_query->vchTitle;
            $this->edLastName->Text = $DB->q_query->vchLastName;
            $this->edEmail->Text = $DB->q_query->vchEmail;
            $this->edSLFirstName->Text = $DB->q_query->vchSLFirstName;
            $this->edSLLastName->Text = $DB->q_query->vchSLLastName;
            $this->edUUID->Text = $DB->q_query->vchUUID;

            $this->hfPublishRLName->Value = $DB->q_query->bPublishRLName;
            $this->hfPublishSLName->Value = $DB->q_query->bPublishSLName;
            $this->hfPublishEmail->Value = $DB->q_query->bPublishEmail;

            $oid = $DB->q_query->org_iOrgID;

            $DB->q_query->Close();

            SetSessionVar("oid", $oid);

            // Populate the org info portion of the form
            $query = "SELECT * FROM org WHERE iOrgID = " . $oid;

            $DB->q_query->SQL = $query;
            $DB->q_query->open();

            $this->edName->Text = $DB->q_query->vchName;
            $this->mDescription->Text = $DB->q_query->vchDescription;
            $this->edURL->Text = $DB->q_query->vchURL;
            $this->edSLURL->Text = $DB->q_query->vchSLURL;
            $this->edLogoFileName->Text = $DB->q_query->vchLogoFileName;
            $this->edNotecardKey->Text = $DB->q_query->vchNotecardKey;

            $cid = $DB->q_query->category_iCategoryID;

            $DB->q_query->Close();

            SetSessionVar("cid", $cid);

            $this->btSubmit->Caption = "Edit";
            $this->btCancel->Enabled = false;

            $this->edFirstName->ReadOnly = true;
            $this->edTitle->ReadOnly = true;
            $this->edLastName->ReadOnly = true;
            $this->edEmail->ReadOnly = true;
            $this->edSLFirstName->ReadOnly = true;
            $this->edSLLastName->ReadOnly = true;
            $this->edUUID->ReadOnly = true;

            $this->edName->ReadOnly = true;
            $this->mDescription->ReadOnly = true;
            $this->edURL->ReadOnly = true;
            $this->edSLURL->ReadOnly = true;
            $this->edLogoFileName->ReadOnly = true;
            $this->edNotecardKey->ReadOnly = true;

            $this->cbCategory->Enabled = false;

            $this->ibSLName->Enabled = false;
            $this->ibRLName->Enabled = false;
            $this->ibEmail->Enabled = false;
         }

         if($action == "edit")
         {
            // $cid = $this->cbCategory->ItemIndex;

            //Get the cid for this org from the session since the drop down list
            //loses it
            $cid = GetSessionVar('cid');

            $this->btSubmit->Caption = "Save";
            $this->btCancel->Enabled = true;

            $this->edFirstName->ReadOnly = false;
            $this->edTitle->ReadOnly = false;
            $this->edLastName->ReadOnly = false;
            $this->edEmail->ReadOnly = false;
            $this->edSLFirstName->ReadOnly = false;
            $this->edSLLastName->ReadOnly = false;
            $this->edUUID->ReadOnly = false;

            $this->edName->ReadOnly = false;
            $this->mDescription->ReadOnly = false;
            $this->edURL->ReadOnly = false;
            $this->edSLURL->ReadOnly = false;
            $this->edLogoFileName->ReadOnly = false;
            $this->edNotecardKey->ReadOnly = false;

            $this->cbCategory->Enabled = true;

            $this->ibSLName->Enabled = true;
            $this->ibRLName->Enabled = true;
            $this->ibEmail->Enabled = true;
         }

         // Populate categories drop-down list
         $query = "SELECT iCategoryID, vchName FROM category ORDER BY vchName ASC";

         $DB->q_query->Close();
         $DB->q_query->SQL = $query;
         $DB->q_query->Open();

         $this->populate_list_element($this->cbCategory, $DB->q_query, "vchName", "iCategoryID", $cid);

         $DB->q_query->Close();

         // Initialize privacy option image buttons
         if($this->hfPublishRLName->Value == "0")
            $this->ibRLName->ImageSource = "locked.png";
         else
            $this->ibRLName->ImageSource = "unlocked.png";

         if($this->hfPublishSLName->Value == "0")
            $this->ibSLName->ImageSource = "locked.png";
         else
            $this->ibSLName->ImageSource = "unlocked.png";

         if($this->hfPublishEmail->Value == "0")
            $this->ibEmail->ImageSource = "locked.png";
         else
            $this->ibEmail->ImageSource = "unlocked.png";

      }

   }

   function populate_list_element($element, $dataset, $fieldname, $indexfield, $setindex)
   {

      $items = array();

      $element->Clear();
      $dataset->Active = true;
      $dataset->First();

      while(!$dataset->EOF)
      {
         $items[$dataset->$indexfield] = $dataset->$fieldname;
         $dataset->Next();
      }

      $element->Items = $items;
      $dataset->Active = false;

      if($setindex >= 0)
         $element->ItemIndex = $setindex;

   }

   function btSubmitClick($sender, $params)
   {

      $action = GetSessionVar('action');

      if($action == "view")
      {
         SetSessionVar("action", "edit");
      }
      else
      {
         SetSessionVar("action", "view");

         global $DB;

         // Store modified person data
         $person_data = array(
                              'vchFirstName' => $this->edFirstName->Text,
                              'vchTitle' => $this->edTitle->Text,
                              'vchLastName' => $this->edLastName->Text,
                              'vchEmail' => $this->edEmail->Text,
                              'vchSLFirstName' => $this->edSLFirstName->Text,
                              'vchSLLastName' => $this->edSLLastName->Text,
                              'vchUUID' => $this->edUUID->Text,
                              'bPublishRLName' => $this->hfPublishRLName->Value,
                              'bPublishSLName' => $this->hfPublishSLName->Value,
                              'bPublishEmail' => $this->hfPublishEmail->Value
                              );

         $msg = '';

         if(empty($msg))
         {
            $DB->q_query->close();

            $query = "UPDATE person SET ";
            $first = true;

            foreach($person_data as $key => $value)
            {
               if($first)
                  $first = false;
               else
                  $query .= ', ';

               $query .= $key . " = '" . mysql_real_escape_string($value) . "'";
            }

            $pid = GetSessionVar('pid');

            $query .= ' WHERE iPersonID = ' . $pid;

            $DB->q_query->LimitStart = '-1';
            $DB->q_query->LimitCount = '-1';
            $DB->q_query->SQL = $query;

            $DB->q_query->open();

            $DB->q_query->close();

         }

         // Store modified org data
         $org_data = array(
                           'vchName' => $this->edName->Text,
                           'vchDescription' => $this->mDescription->Text,
                           'vchURL' => $this->edURL->Text,
                           'vchSLURL' => $this->edSLURL->Text,
                           'vchLogoFileName' => $this->edLogoFileName->Text,
                           'vchNotecardKey' => $this->edNotecardKey->Text,
                           'category_iCategoryID' => $cid = $this->cbCategory->ItemIndex
                           );

         $msg = '';

         /* foreach( $org_data as $key => $value )
         {
         if( empty( $value ) )
         {
         $msg .= $key . ' field must not be blank!<br>';
         break;
         }
         } */

         if(empty($msg))
         {
            $query = "UPDATE org SET ";
            $first = true;

            foreach($org_data as $key => $value)
            {
               if($first)
                  $first = false;
               else
                  $query .= ', ';

               $query .= $key . " = '" . mysql_real_escape_string($value) . "'";
            }

            $oid = GetSessionVar('oid');

            $query .= ' WHERE iOrgID = ' . $oid;

            $DB->q_query->LimitStart = '-1';
            $DB->q_query->LimitCount = '-1';
            $DB->q_query->SQL = $query;

            $DB->q_query->open();

            $DB->q_query->close();
         }
      }
   }
}

global $application;

global $Org;

//Creates the form
$Org = new Org($application);

//Read from resource file
$Org->loadResource(__FILE__);

//Shows the form
$Org->show();

?>